﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    /// <summary>
    /// 申请成为作者，需要补充以下数据
    /// </summary>
    [Table("T_Author")]
    public class T_Author : IEntity<T_Author>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string UserName { get; set; }
        /// <summary>
        /// 名
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string FirstName { get; set; }
        /// <summary>
        /// 姓
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(32)]
        public string LastName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        [Column(TypeName = "bit")]
        public bool Gender { get; set; }
        
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Email { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Department { get; set; }
        /// <summary>
        /// 单位英文
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Department_EN { get; set; }
        /// <summary>
        /// 称谓
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Title { get; set; }
        /// <summary>
        /// 称谓英文
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Title_EN { get; set; }
        /// <summary>
        /// 作者简介
        /// </summary>
        [Column(TypeName = "text")]
        public string Introduction { get; set; }
        /// <summary>
        /// 作者简介英文
        /// </summary>
        [Column(TypeName = "text")]
        public string Introduction_EN { get; set; }
        /// <summary>
        /// 查稿密码
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string CheckPassword { get; set; }
        /// <summary>
        /// 详细地址
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Address { get; set; }
        /// <summary>
        /// 邮政编码
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string PostCode { get; set; }
        /// <summary>
        /// 联系电话
        /// </summary>
        [Column(TypeName="varchar")]
        [StringLength(32)]
        public string Phone { get; set; }
        /// <summary>
        /// 国籍
        /// </summary>
        public int Country { get; set; }
        /// <summary>
        /// 学位
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Degree { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        public override T_Author Find(System.Linq.Expressions.Expression<Func<T_Author, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Author.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.CreateTime = item.CreateTime;
                    this.Department = item.Department;
                    this.Department_EN = item.Department_EN;
                    this.Gender = item.Gender;
                    this.Introduction = item.Introduction;
                    this.Introduction_EN = item.Introduction_EN;
                    this.FirstName = item.FirstName;
                    this.LastName = item.LastName;
                    this.Title = item.Title;
                    this.Title_EN = item.Title_EN;
                    this.UserName = item.UserName;
                    this.CheckPassword = item.CheckPassword;
                    this.Address=item.Address;
                    this.PostCode =item.PostCode;
                    this.Country=item.Country;
                    this.Degree=item.Degree;
                    this.Email = item.Email;
                    this.Phone = item.Phone;
                    return this; 
                }
                return null;
            }
        }
        public override T_Author Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_Author, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Author.Any(where);
            }
        }
    }
}
